package admage.model.reportapi.aid;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.MalformedInputException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 * @author okada
 *
 */
public class AtndRSS {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws MalformedInputException,
			ProtocolException, IOException {

		URL url =
			new URL("http://atnd.org/events.rss");
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		urlConn.setRequestMethod("GET");
		urlConn.setInstanceFollowRedirects(false);
		urlConn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");

		urlConn.connect();

		Document doc = null;
		try {
			doc = getDocumet(urlConn.getInputStream());
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		// ルートの要素名になっている子ノードを取得する
		Element root = doc.getDocumentElement();
		System.out.println("ルート要素名：" + root.getTagName());

		// 各ノードリストを取得
		NodeList nodeList = root.getElementsByTagName("channel");
		System.out.println("ノードリストの数は：" + nodeList.getLength());

		for (int i = 0; i < nodeList.getLength(); i++) {
			Element element1 = (Element)nodeList.item(i);
			//

			System.out.println(getChildren(element1, "title"));


			System.out.println(getChildren(element1, "description"));

			System.out.println(getChildren(element1, "pubDate"));

			// 各ノードリストを取得
			NodeList list = element1.getElementsByTagName("item");
			System.out.println("リストの数は：" + nodeList.getLength());
			for (int j = 0; j< list.getLength(); j++) {
				Element element2 = (Element)list.item(j);
				System.out.println(getChildren(element2, "title"));

				System.out.println("author：" + getChildren(element2, "author"));
				System.out.println("Link：" + getChildren(element2, "link"));

				System.out.println("description：" + getChildren(element2, "description"));

				System.out.println(getChildren(element2, "pubDate"));
			}
		}

		urlConn.disconnect();
	}

	private static Document getDocumet(InputStream is) throws SAXException,
			IOException, ParserConfigurationException {

		DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		return docbuilder.parse(is);

	}

	  /**
	   * 指定されたエレメントから子要素の内容を取得。
	   *
	   * @param   element 指定エレメント
	   * @param   tagName 指定タグ名
	   * @return  取得した内容
	   */
	  public static String getChildren(Element element, String tagName) {
	    NodeList list = element.getElementsByTagName(tagName);
	    Element cElement = (Element)list.item(0);
	    return cElement.getFirstChild().getNodeValue();
	  }

}